'use strict';
const cloud = require('wx-server-sdk')
cloud.init({
	env: 'dev-avbw2'
})

const uniAccount = require('@dcloudio/uni-account')
const uniAccountIns = uniAccount.initWeixin({
	appId: "wxebfaf0efe01e1460",
	secret: "554d5aceb76e2d2c9f1cd869166da330"
})

// const db = uniCloud.database();
let response = function(code, data, msg) {
	return {
		code: code,
		data: data,
		msg: msg || '成功'
	}
}

exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	let cloudInfo = cloud.getWXContext()
	console.log('cloudInfo : ', cloudInfo)
	const {
		openid
	} = await uniAccountIns.code2Session(event.code)
	console.log('openid : ', openid)
	//返回数据给客户端
	return response(10000, {
		...context,
		...cloudInfo,
		...event,
		openid
	}, '成功');
};
